package cn.com.twoke.application.game.mario.core.ecs.component;

import cn.com.twoke.application.game.mario.core.ecs.util.PoolManager;

public class Vector2f implements PoolManager.Resetable {
    public float x;
    public float y;

    // 构造函数
    public Vector2f() {
        this(0, 0);
    }

    public Vector2f(float x, float y) {
        this.x = x;
        this.y = y;
    }

    // 向量归一化 (单位化)
    public Vector2f normalize() {
        float length = length();
        if (length != 0) {
            x /= length;
            y /= length;
        }
        return this;
    }

    // 返回归一化后的新向量(不改变原向量)
    public Vector2f normalized() {
        Vector2f result = new Vector2f(x, y);
        return result.normalize();
    }

    // 点积 (内积)
    public float dot(Vector2f other) {
        return x * other.x + y * other.y;
    }

    // 向量长度(模)
    public float length() {
        return (float)Math.sqrt(x * x + y * y);
    }

    // 向量长度的平方(避免开方计算)
    public float lengthSquared() {
        return x * x + y * y;
    }

    // 向量加法
    public Vector2f add(Vector2f other) {
        x += other.x;
        y += other.y;
        return this;
    }

    // 向量减法
    public Vector2f sub(Vector2f other) {
        x -= other.x;
        y -= other.y;
        return this;
    }

    // 向量乘法(标量)
    public Vector2f mul(float scalar) {
        x *= scalar;
        y *= scalar;
        return this;
    }

    // 向量除法(标量)
    public Vector2f div(float scalar) {
        x /= scalar;
        y /= scalar;
        return this;
    }

    // 计算两个向量之间的距离
    public static float distance(Vector2f a, Vector2f b) {
        float dx = a.x - b.x;
        float dy = a.y - b.y;
        return (float)Math.sqrt(dx * dx + dy * dy);
    }

    // 线性插值
    public static Vector2f lerp(Vector2f a, Vector2f b, float t) {
        t = Math.max(0, Math.min(1, t)); // 限制t在[0,1]范围
        return new Vector2f(
                a.x + (b.x - a.x) * t,
                a.y + (b.y - a.y) * t
        );
    }

    // 向量旋转(弧度)
    public Vector2f rotate(float radians) {
        float cos = (float)Math.cos(radians);
        float sin = (float)Math.sin(radians);
        float newX = x * cos - y * sin;
        float newY = x * sin + y * cos;
        x = newX;
        y = newY;
        return this;
    }

    // 判断向量是否为零向量
    public boolean isZero() {
        return x == 0 && y == 0;
    }

    // 设置向量值
    public Vector2f set(float x, float y) {
        this.x = x;
        this.y = y;
        return this;
    }

    // 复制向量
    public Vector2f copy() {
        return new Vector2f(x, y);
    }

    @Override
    public String toString() {
        return String.format("(%.2f, %.2f)", x, y);
    }
    
    /**
     * 重置对象状态
     */
    @Override
    public void reset() {
        this.x = 0;
        this.y = 0;
    }

    /**
     * 从对象池获取Vector2f实例
     *
     * @param x x坐标
     * @param y y坐标
     * @return Vector2f实例
     */
    public static Vector2f obtain(float x, float y) {
        return PoolManager.acquireVector2f(x, y);
    }

    /**
     * 归还Vector2f实例到对象池
     *
     * @param vector Vector2f实例
     */
    public static void release(Vector2f vector) {
        PoolManager.releaseVector2f(vector);
    }
}